<?php
	include_once 'isLogin.php';
	$advc=new adv();
	$id=$_GET['id'];		//ID(修改)	
	$table_adv_position="adv_position"	; //广告位

	//修改
	if($id!='' && $id!=null){
		$row=$advc->adv_info($id);
		//读取位置
		$position=$commonc->getInfo($table_adv_position,$row['position_id']);
		$row['position']=$position['position'];
		$row['width']=$position['width'];
		$row['height']=$position['height'];

		$smarty->assign('row',$row);
	}

	if($_POST['submit']){
		//上传图片，生成缩略图
		$adv_img=$row['adv_img'];
		if($_FILES['adv_img']['name']!=''){
			//设置路径
			$folder = "../upload/adv/";
			$annexFolder = $folder."large/";
			$smallFolder = $folder."small/";

			//删除旧图
			@unlink($annexFolder.$adv_img);
			@unlink($smallFolder.$adv_img);
			//上传新图
			$upimg = new uploadImg();
			$adv_img = $upimg->upLoad('adv_img',$annexFolder);
			//小图
			$upimg->smallImg($annexFolder.$adv_img,$smallFolder);
		}

		$arr= array('adv_name' =>$_POST['adv_name'],
					'adv_link' =>$_POST['adv_link'],
					'adv_img' =>$adv_img,
			 );

		if($id!='' && $id!=null){//update
			$res=$advc->adv_update($id,$arr);	
		}else{//insert
			$arr['position_id']=$_POST['position_id'];
			$res=$advc->adv_add($arr);	
		}
		
		if($res){
			$msg="操作成功";
			$href="adv_list.php";
		}else{
			$msg="操作失败";
			$href="adv_list.php";
		}
		echo $commonc->submitAlert($msg,$href);
		exit;
	}

	//读取位置
	$position=$advc->adv_position_list();
	$smarty->assign('position',$position['result']);


	
	$smarty->display("adv_add.html");
?>